package com.yzt.dynamicmodule.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.yzt.dynamicmodule.R;
import com.yzt.dynamicmodule.adapter.SearchUserAdapter;
import com.yzt.zhuju.arouter.RouterSayu;
import com.yzt.zhuju.arouter.RouterUser;
import com.yzt.zhuju.base.BaseActivity;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.home.SearchUserBean;
import com.yzt.zhuju.widget.tuiview.TUIEditText;
//import com.yzt.businessbasemodule.adapter.SearchUserAdapter;
import com.yzt.p_base.utils.ToastUtil;

import java.util.List;

/**
 * author : bob
 * date   : 2022/12/1
 * desc   : 搜索页面
 */
@Route(path = RouterSayu.ACTIVITY_INDEX_SEARCH)
public class SearchUserActivity extends BaseActivity implements View.OnClickListener {

    private TUIEditText editText;

    private TextView tvCancel;

    private SearchUserAdapter searchUserAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_search_user;
    }

    @Override
    public void installViews() {

        editText = findViewById(R.id.et_id);
        tvCancel = findViewById(R.id.tv_cancel);
        RecyclerView mListRv = findViewById(R.id.list_rv);
        tvCancel.setOnClickListener(this);

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String content = editText.getText().toString().trim();
                tvCancel.setText("".equals(content) ? getString(R.string.cancel) : getString(R.string.search));
            }
        });

        editText.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                String userId = editText.getText().toString().trim();
                if (!TextUtils.isEmpty(userId)) {
                    searchUserInfo(userId);
                    return true;
                }
            }
            return false;
        });

        mListRv.setLayoutManager(new LinearLayoutManager(mActivity));
        searchUserAdapter = new SearchUserAdapter();
        mListRv.setAdapter(searchUserAdapter);
        searchUserAdapter.setOnItemClickListener((adapter, view, position) -> {
            String uId = searchUserAdapter.getData().get(position).getUid();
            RouterUser.PersonalDetailActivity(uId);
        });
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id = view.getId();
        if (id == R.id.tv_cancel) {
            String userId = editText.getText().toString().trim();
            if ("".equals(userId)) {
                finish();
                return;
            }
            searchUserInfo(userId);
        }
    }

    /**
     * 根据ID 搜索用户信息
     */
    public void searchUserInfo(String userId) {
        BizLayer.getInstance().getAccountModule().searchUser(userId, mCurrentPage, PAGE_SIZE, new AresSubscriber<List<SearchUserBean>>() {
            @Override
            protected void onSuccess(List<SearchUserBean> list) {
                searchUserAdapter.setList(list);
                if (list.isEmpty()) {
                    ToastUtil.showMessage(getString(R.string.search_empty_data));
                }
            }
        });
    }
}

